<?php

namespace App\Http\Controllers;

use App\Http\Requests;

class UploadController extends Controller
{

    /**
     * 上传
     */
    public function upload()
    {
        $file = $_FILES['Filedata'];
        if($file['error'] === 0){
            if($file['type'] == 'application/octet-stream'){
                $root_path = $_SERVER['DOCUMENT_ROOT']."/".env('UPLOAD');
                $relative_path = date('Y').'/'.date('m').'/'.date('d').'/';
                $save_path = $root_path.'/image/'.$relative_path;
                if(!is_dir($save_path)){
                    $r = mkdir($save_path,0755,true);
                    if(!$r){
                        echo json_encode(array('status'=>3,'info'=>'创建目录失败'.$root_path));exit;
                    }
                }
                $thumb_path = $root_path.'/thumb/'.$relative_path;
                if(!is_dir($thumb_path)){
                    $r = mkdir($thumb_path,0755,true);
                    if (!$r){
                        echo json_encode(array('status'=>3,'info'=>'创建目录失败'.$thumb_path));exit;
                    }
                }
                $tmp_name = $file['tmp_name'];
                $name = $file['name'];
                $file_name = md5_file($tmp_name);
                $suffix = $this->getFileSuffix($name);

                $im = null;
                if ($suffix == 'jpg' || $suffix == 'jpeg'){
                    $im = imagecreatefromjpeg($tmp_name);
                }elseif ($suffix == 'gif'){
                    $im = imagecreatefromgif($tmp_name);
                }elseif ($suffix == 'png'){
                    $im = imagecreatefrompng($tmp_name);
                }
                if ($im){
                    $photo1 = $file_name.'1';
                    $photo2 = $file_name.'2';
                    $photo3 = $file_name.'3';
                    $width1 = env('WIDTH1');
                    $height1 = env('HEIGHT1');
                    $width2 = env('WIDTH2');
                    $height2 = env('HEIGHT2');
                    $width3 = env('WIDTH3');
                    $height3 = env('HEIGHT3');
                    $this->ResizeImage($im, $width1, $height1, $thumb_path, $photo1);
                    $this->ResizeImage($im, $width2, $height2, $thumb_path, $photo2);
                    $this->ResizeImage($im, $width3, $height3, $thumb_path, $photo3);
                    ImageDestroy($im);
                }

                $m = move_uploaded_file($tmp_name,"$save_path$file_name.$suffix");
                $path = "image/$relative_path$file_name.$suffix";
                $photo1_path = 'thumb/'.$relative_path.$file_name.'1.'.$suffix;
                $photo2_path = 'thumb/'.$relative_path.$file_name.'2.'.$suffix;
                $photo3_path = 'thumb/'.$relative_path.$file_name.'3.'.$suffix;
                if($m){
//					echo json_encode(array('status'=>0,'info'=>'上传成功'.$save_path,'path'=>$path,'name'=>$name,'size'=>$file['size']));
                    echo json_encode(array('status'=>0,'info'=>'上传成功','path'=>$path,'photo1'=>$photo1_path,'photo2'=>$photo2_path,'photo3'=>$photo3_path));
                }else{
                    echo json_encode(array('status'=>4,'info'=>'上传失败'));
                }
            }else{
                json_encode(array('status'=>2,'info'=>'上传文件不合法'));
            }
        }else{
            json_encode(array('status'=>1,'info'=>'上传出错'));
        }
    }

    /**
     * 获取文件后缀
     */
    private function getFileSuffix($filename)
    {
        $fileinfo = pathinfo($filename);
        $suffix = $fileinfo['extension'];
        return strtolower($suffix);
    }

    /**
     * 生成缩略图
     */
    private function ResizeImage($im,$maxwidth,$maxheight,$path,$name)
    {
        $width = imagesx($im);
        $height = imagesy($im);
        if (($maxwidth && $width > $maxwidth) || ($maxheight && $height > $maxheight)) {
            if ($maxwidth && $width > $maxwidth) {
                $widthratio = $maxwidth / $width;
                $RESIZEWIDTH = true;
            }
            if ($maxheight && $height > $maxheight) {
                $heightratio = $maxheight / $height;
                $RESIZEHEIGHT = true;
            }
            if ($RESIZEWIDTH && $RESIZEHEIGHT) {
                if ($widthratio < $heightratio) {
                    $ratio = $widthratio;
                } else {
                    $ratio = $heightratio;
                }
            } elseif ($RESIZEWIDTH) {
                $ratio = $widthratio;
            } elseif ($RESIZEHEIGHT) {
                $ratio = $heightratio;
            }
            $newwidth = $width * $ratio;
            $newheight = $height * $ratio;
            if (function_exists("imagecopyresampled")) {
                $newim = imagecreatetruecolor($newwidth, $newheight);
                imagecopyresampled($newim, $im, 0, 0, 0, 0, $newwidth, $newheight, $width, $height);
            } else {
                $newim = imagecreate($newwidth, $newheight);
                imagecopyresized($newim, $im, 0, 0, 0, 0, $newwidth, $newheight, $width, $height);
            }
            ImageJpeg($newim, $path.$name.".jpg");
            ImageDestroy($newim);
        } else {
            ImageJpeg($im, $path.$name.".jpg");
        }
        return true;
    }

}
